<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4、使⽤ JS 闭包，给 10 个 p 标签循环加上点击事件 </title>
</head>

<body>
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p>4</p>
  <p>5</p>
  <p>6</p>
  <p>7</p>
  <p>8</p>
  <p>9</p>
  <p>10</p>
  <script>
    window.onload = () => {
      let p = document.getElementsByTagName('p')
      function closure(i) {
        for (let i = 0; i < p.length; i++) {
          p[i].onclick = function () {
            console.log(p[i]);
          };
        }
      }
      closure()
    }

  </script>
</body>

</html>